local tianbian = fk.CreateSkill {

  name = "joy__tianbian",

  tags = {  },

}



tianbian:addEffect(fk.StartPindian, {
  name = "joy__tianbian",
  anim_type = "special",
  events ={fk.StartPindian, fk.PindianCardsDisplayed},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(tianbian.name) then
      if event == fk.StartPindian then
        return player == data.from or table.contains(data.tos, player)
      else
        if player == data.from then
          return data.fromCard.suit == Card.Heart
        elseif data.results[player.id] then
          return data.results[player.id].toCard.suit == Card.Heart
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.StartPindian then
      return player.room:askForSkillInvoke(player, tianbian.name, nil, "#joy__tianbian-invoke")
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.StartPindian then
      if player == data.from then
        data.fromCard = Fk:getCardById(player.room.draw_pile[1])
      else
        data.results[player.id] = data.results[player.id] or {}
        data.results[player.id].toCard = Fk:getCardById(player.room.draw_pile[1])
      end
    else
      if player == data.from then
        data.fromCard.number = 13
      elseif data.results[player.id] then
        data.results[player.id].toCard.number = 13
      end
    end
  end,
})
tianbian:addEffect(fk.PindianCardsDisplayed, {
  name = "joy__tianbian",
  anim_type = "special",
  events ={fk.StartPindian, fk.PindianCardsDisplayed},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(tianbian.name) then
      if event == fk.StartPindian then
        return player == data.from or table.contains(data.tos, player)
      else
        if player == data.from then
          return data.fromCard.suit == Card.Heart
        elseif data.results[player.id] then
          return data.results[player.id].toCard.suit == Card.Heart
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.StartPindian then
      return player.room:askForSkillInvoke(player, tianbian.name, nil, "#joy__tianbian-invoke")
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.StartPindian then
      if player == data.from then
        data.fromCard = Fk:getCardById(player.room.draw_pile[1])
      else
        data.results[player.id] = data.results[player.id] or {}
        data.results[player.id].toCard = Fk:getCardById(player.room.draw_pile[1])
      end
    else
      if player == data.from then
        data.fromCard.number = 13
      elseif data.results[player.id] then
        data.results[player.id].toCard.number = 13
      end
    end
  end,
})

return tianbian